package com.ruoyi.web.controller.applets;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.task.domain.Task;
import com.ruoyi.task.service.ITaskService;
import com.ruoyi.wechat.domain.WechatUser;
import com.ruoyi.wechat.service.IWechatUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.HttpStatus.LOGOUT;
import static com.ruoyi.common.constant.HttpStatus.RESOURCE_LIMIT;

/**
 * 小程序任务Controller
 *
 * @author KevenPotter
 * @date 2024-04-30 09:40:15
 */
@RestController
@RequestMapping("/applets/task/task")
@AllArgsConstructor
public class AppletsTaskController extends BaseController {

    private final ITaskService taskService;
    private final IWechatUserService wechatUserService;
    private final TokenService tokenService;

    /**
     * 查询任务列表
     *
     * @param task 任务对象
     * @return 返回任务列表
     * @author KevenPotter
     * @date 2024-04-30 09:41:20
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Task task) {
        task.setReviewStatus(99);
        startPage();
        List<Task> list = taskService.getList(task);
        list.forEach(item -> taskService.filterOutbound(item, null));
        return getDataTable(list);
    }

    /**
     * 查询自身的任务列表
     *
     * @param task 任务对象
     * @return 返回任务列表
     * @author KevenPotter
     * @date 2024-07-23 09:53:16
     */
    @Anonymous
    @GetMapping("/listForSelf")
    public TableDataInfo listForSelf(Task task) {
        if (tokenService.checkAppletsToken()) {
            WechatUser loginWechatUser = tokenService.getLoginWechatUser();
            task.setPublishUserId(loginWechatUser.getId());
            startPage();
            List<Task> list = taskService.getList(task);
            list.forEach(item -> taskService.filterOutbound(item, null));
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 查询前三精选任务列表
     *
     * @param task 任务对象
     * @return 返回前三精选任务列表
     * @author KevenPotter
     * @date 2024-04-30 09:15:51
     */
    @Anonymous
    @GetMapping("/listTop3Featured")
    public TableDataInfo listTop3Featured(Task task) {
        task.setType("2").setReviewStatus(99);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<Task> list = taskService.getList(task);
        list = list.stream().limit(3).collect(Collectors.toList());
        list.forEach(item -> taskService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 查询精选任务列表
     *
     * @param task 任务对象
     * @return 返回精选任务列表
     * @author KevenPotter
     * @date 2024-04-30 09:15:51
     */
    @Anonymous
    @GetMapping("/listFeatured")
    public TableDataInfo listFeatured(Task task) {
        task.setType("2").setReviewStatus(99);
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        List<Task> list = taskService.getList(task);
        list.forEach(item -> taskService.filterOutbound(item, loginUser));
        return getDataTable(list);
    }

    /**
     * 根据[任务编号]获取任务详情
     *
     * @author KevenPotter
     * @date 2024-05-02 15:52:17
     */
    @Anonymous
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        Task task = taskService.getById(taskId);
        task = taskService.filterOutbound(task, null);
        return success(task);
    }

    /**
     * 新增任务
     *
     * @param task 任务对象
     * @return 返回是否新增成功
     * @author KevenPotter
     * @date 2023-04-30 10:02:14
     */
    @Anonymous
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Task task) {
        if (tokenService.checkAppletsToken()) {
            WechatUser loginWechatUser = tokenService.getLoginWechatUser();
            WechatUser realWechatUser = wechatUserService.getById(loginWechatUser.getId());
            if (realWechatUser.getPoints() <= 0) return AjaxResult.error(RESOURCE_LIMIT, "您的积分不足，请添加积分");
            task.setPublishUserId(loginWechatUser.getId());
            task = taskService.filterInbound(task, null);
            int i = taskService.insertTask(task);
            if (i > 0) wechatUserService.setPoints(realWechatUser.getId(), realWechatUser.getPoints() - 1);
            return toAjax(i);
        }
        return AjaxResult.error(LOGOUT, "请重新登录");
    }

}
